/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.jmarias.server;

import java.util.*;
import com.google.code.jmarias.common.*;
import com.google.code.jmarias.server.comparator.JustValueComparator;
import com.google.code.jmarias.server.tcp.RemoteContactableUser;

/**
 * trida, ktera oznacuje herni stul, provadejici zakladni akce
 * @author josef
 */
public class Table {

    private List<Card> trick = new ArrayList<Card>(); //stych
    private static Table table;
    private Stack<Card> deck;
    private Suit trumpSuit;
    private Card[] sevenCards = new Card[7];
    private List<Card> talon = new ArrayList<Card>();
    private Card[][] otherCards = new Card[5][5];
    private Player[] players = new Player[3];
    Map<Integer, RemoteContactableUser> clients;

    public List<Card> getTrick() {
        return trick;
    }

    public List<Card> getTalon() {
        return talon;
    }

    public void setTalon(List<Card> talon) {
        this.talon = talon;
    }

    public void setTrumpSuit(Suit trumpSuit) {
        this.trumpSuit = trumpSuit;
    }

    public Stack<Card> getDeck() {
        return deck;
    }

    public void setDeck(Stack<Card> deck) {
        this.deck = deck;
    }

    public Card popDeck() {
        return deck.pop();
    }

    public Card[][] getOtherCards() {
        return otherCards;
    }

    public Card[] getFiveCards(int argA) {
        return otherCards[argA];
    }

    public Card[] getSevenCards() {
        return sevenCards;
    }

    public Map<Integer, RemoteContactableUser> getClients() {
        return clients;
    }

    public void setClients(Map<Integer, RemoteContactableUser> clients) {
        this.clients = clients;
    }

    public void createDeck() {
        List<Card> tempDeck = new ArrayList<Card>();
        for (Suit suit : Suit.values()) {
            for (Value value : Value.values()) {
                tempDeck.add(new Card(suit, value));
            }
        }
        deck = shuffleDeck(tempDeck);
    }

    /**
     * zamicha balicek karet
     *  @return Zasobnik! - zamichane karty
     */
    private Stack<Card> shuffleDeck(List<Card> inputDeck) {
        Stack<Card> result = new Stack<Card>();
        List<Card> tempDeck = inputDeck;
        Random random = new Random();
        while (!tempDeck.isEmpty()) {
            int randInt = random.nextInt(tempDeck.size());
            result.push(tempDeck.get(randInt));
            tempDeck.remove(randInt);
        }
        return result;
    }

    /**
     * rozdeli karty do skupinek po 7,5,5,5,5,5 pro rozesilani jednotlivym hracum
     */
    public void prepareCards() {
        for (int i = 0; i < 7; i++) {
            sevenCards[i] = deck.pop();
        }
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                otherCards[i][j] = deck.pop();
            }
        }
    }

    public com.google.code.jmarias.server.Player[] getPlayers() {
        return players;
    }

    public void initializePlayers() {
        Player pepa = new Player(0, "Pepa");
        pepa.addCardsToHand(otherCards[2]);
        players[0] = pepa;
//        players[0].addCardsToHand(otherCards[2]);
//        players[1].addCardsToHand(otherCards[0]);
//        players[1].addCardsToHand(otherCards[3]);
//        players[2].addCardsToHand(otherCards[1]);
//        players[2].addCardsToHand(otherCards[4]);
    }

    public Card getHighestTrickCard() {
        if (trickHasTrumps()) {
            return getHighestCardOfSuit(trumpSuit, trick);
        } else {
            return getHighestCardOfSuit(getTrickSuit(), trick);
        }
    }

    public Card getHighestCardOfSuit(Suit suit, List<Card> list) {
        Card result = list.get(0);
        JustValueComparator valueComparer = new JustValueComparator();
        for (Card card : list) {
            if (card.getSuit() == suit && valueComparer.compare(card, result) > 0) {
                result = card;
            }
        }
        return result;
    }

    private boolean trickHasTrumps() {
        for (Card card : trick) {
            if (card.getSuit() == trumpSuit) {
                return true;
            }
        }
        return false;
    }

    public Suit getTrickSuit() {
        return trick.get(0).getSuit();
    }
}
